package OddMuse;

=head1 NAME
    
OddMuse::Database::Building - Perl Module for OddMuse database building

=head1 DESCRIPTION

This module does the grunt work in building an indexable database for OddMuse.

=head1 DEPENDENCIES

L<MLDBM>, L<OddMuse::Database>, L<OddMuse::Database::Logging>

=cut

$VERSION = ( q$Revision: 31 $ ) =~ /(\d+)/g;
use OddMuse::Database qw( $datafile $dataswap $debug );
use OddMuse::Database::Logging;  # Give us logging!
use MLDBM qw( DB_File Storable );
use strict;

=head1 GLOBAL VARIABLES

=over 2

=item * %PageCache

Used to temporarily cache page information.  It's mostly used (at the moment) to
speed up the generation of backlinks.

=back

=cut

our %PageCache;  # Used to build backlinks

=head1 ADMIN MENU

A new option "Rebuild Database for Searching" is added to the admin menu

=cut

push( @MyAdminCode, \&DataBaseBuild );
sub DataBaseBuild {
    my ( $id, $menuref, $restref ) = @_;
    push( @$menuref,
          ScriptLink( 'action=metabuild',
                      T( 'Rebuild Database for searching' ), 'metabuild'
                    )
        );
} ## end sub DataBaseBuild


=head1 ACTIONS

=over 2

=item * metabuild

Calling /?action=metabuild will begin compile process

=back

=cut

$Action{ metabuild } = \&buildDatabase;

=head1 FUNCTIONS

=over 2

=item * buildDatabase

This function first loops though the OddMuse %page hash and transfers all the
data from it into the %PageCache hash.  

Once that's been accomplished, it then creates a new database record via the
OddMuse::Database->parse method, pushing much of the gathered info into the
parse method for filtering.  A completed hash reference in returned, which can
then be tied to the on-disk datastore.

Finally, the store is flushed and synced, then swapped out with the running
version.

=back

=cut 

sub buildDatabase {
    my $class = shift;
    my $self  = {};
    my $timer = time; # Start Timer

    print GetHttpHeader( 'text/plain' );

    &OddMuse::Database::log1( "Pre-Caching Wiki Pages for Data Store Process ...\n" );
    &OddMuse::Database::log4( "Pre-Caching Wiki Pages for Data Store Process ...\n" );

    foreach my $name ( AllPagesList() ) {
        OpenPage( $name );
        &OddMuse::Database::log2( "Caching $name ...\n" );
        $PageCache{ $name } = $Page{ text };
    }

    &OddMuse::Database::log4(
        "\nStarting Database Store Process ... this takes a while ... please wait\n"
    );
    &OddMuse::Database::log1(
        "\nStarting Database Store Process ... this takes a while ... please wait\n"
    );

    tie my %database, 'MLDBM', $dataswap
      or die "Cannot open file $dataswap $!\n";


    foreach my $name ( AllPagesList() ) {
        OpenPage( $name );

        my $record = OddMuse::Database->newrecord;
        $record->parse( $name,
                        $PageCache{ $name },
                        $Page{ username },
                        $Page{ ts },
                        $Page{ originalAuthor },
                        $Page{ created } );    # Fill object with data

        #Link tied hash to object, and whamo! done!
        $database{ $name } = $record;

    } ## end foreach my $name ( AllPagesList...

    if ( $debug >= 4 ) {
        print "\n";
        use Data::Dumper;
        print Dumper( %database );
    }

    untie %database;
    rename( $dataswap, $datafile )
      ;    # Swap out databases, so we have instant update to new data

    my $timetook = time - $timer;

    &OddMuse::Database::log1( "\nDone. $timetook seconds \n" );
    &OddMuse::Database::log4( "\nDone. $timetook seconds \n" );

    return $self;
} ## end sub buildDatabase

1;

__END__

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<cmauch@gmail.com>

=head1 AUTHOR

Charles Mauch <cmauch@gmail.com>

=head1 LICENSE

Copyright (c) 2006 Charles Mauch

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 SEE ALSO

perl(1).

=cut

# $Id: Building.pm 31 2006-09-29 06:21:37Z cmauch $
